home *** CD-ROM | disk | FTP | other *** search
/ PC Answers 1995 May / PC Answers CD-ROM 7 (Future Publishing) (May 1995).iso / vbits / code / mee / vbdao / visdata / opendb.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1994-10-06  |  10.3 KB  |  338 lines

  1. VERSION 2.00
  2. Begin Form fOpenDB 
  3.    BackColor       =   &H00C0C0C0&
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Open DataBase"
  6.    ClientHeight    =   2160
  7.    ClientLeft      =   2460
  8.    ClientTop       =   3840
  9.    ClientWidth     =   4395
  10.    ControlBox      =   0   'False
  11.    ForeColor       =   &H00C0C0C0&
  12.    Height          =   2565
  13.    Left            =   2400
  14.    LinkTopic       =   "Form2"
  15.    MaxButton       =   0   'False
  16.    MinButton       =   0   'False
  17.    ScaleHeight     =   2119.728
  18.    ScaleMode       =   0  'User
  19.    ScaleWidth      =   4447.083
  20.    Top             =   3495
  21.    Width           =   4515
  22.    Begin ComboBox cDBName 
  23.       BackColor       =   &H00FFFFFF&
  24.       Height          =   300
  25.       Left            =   1680
  26.       Sorted          =   -1  'True
  27.       TabIndex        =   0
  28.       Tag             =   "OLS"
  29.       Top             =   105
  30.       Width           =   2655
  31.    End
  32.    Begin TextBox cDataBase 
  33.       BackColor       =   &H00FFFFFF&
  34.       Height          =   285
  35.       Left            =   1680
  36.       TabIndex        =   1
  37.       Tag             =   "OLS"
  38.       Top             =   465
  39.       Width           =   2655
  40.    End
  41.    Begin TextBox cUserName 
  42.       BackColor       =   &H00FFFFFF&
  43.       Height          =   285
  44.       Left            =   1680
  45.       TabIndex        =   2
  46.       Tag             =   "OLS"
  47.       Top             =   825
  48.       Width           =   2655
  49.    End
  50.    Begin TextBox cPassword 
  51.       BackColor       =   &H00FFFFFF&
  52.       Height          =   285
  53.       Left            =   1680
  54.       PasswordChar    =   "*"
  55.       TabIndex        =   3
  56.       Tag             =   "OLS"
  57.       Top             =   1185
  58.       Width           =   2655
  59.    End
  60.    Begin CommandButton OkayButton 
  61.       BackColor       =   &H00C0C0C0&
  62.       Caption         =   "&Open"
  63.       Default         =   -1  'True
  64.       Height          =   375
  65.       Left            =   300
  66.       TabIndex        =   4
  67.       Top             =   1680
  68.       Width           =   1575
  69.    End
  70.    Begin CommandButton CancelButton 
  71.       BackColor       =   &H00C0C0C0&
  72.       Cancel          =   -1  'True
  73.       Caption         =   "&Cancel"
  74.       Height          =   375
  75.       Left            =   2460
  76.       TabIndex        =   5
  77.       Top             =   1680
  78.       Width           =   1575
  79.    End
  80.    Begin Label Label1 
  81.       BackColor       =   &H00C0C0C0&
  82.       Height          =   495
  83.       Left            =   4080
  84.       TabIndex        =   10
  85.       Top             =   1680
  86.       Width           =   375
  87.    End
  88.    Begin Label DataBaseLabel 
  89.       BackColor       =   &H00C0C0C0&
  90.       Caption         =   "DataBase:"
  91.       Height          =   255
  92.       Left            =   120
  93.       TabIndex        =   9
  94.       Top             =   465
  95.       Width           =   1335
  96.    End
  97.    Begin Label DBNameLabel 
  98.       BackColor       =   &H00C0C0C0&
  99.       Caption         =   "Source/Server:"
  100.       Height          =   255
  101.       Left            =   120
  102.       TabIndex        =   6
  103.       Top             =   105
  104.       Width           =   1470
  105.    End
  106.    Begin Label UserNameLabel 
  107.       BackColor       =   &H00C0C0C0&
  108.       Caption         =   "User ID:"
  109.       Height          =   255
  110.       Left            =   120
  111.       TabIndex        =   7
  112.       Top             =   825
  113.       Width           =   1335
  114.    End
  115.    Begin Label PasswordLabel 
  116.       BackColor       =   &H00C0C0C0&
  117.       Caption         =   "Password:"
  118.       Height          =   255
  119.       Left            =   120
  120.       TabIndex        =   8
  121.       Top             =   1170
  122.       Width           =   1335
  123.    End
  124. Option Explicit
  125. Dim BeenLoaded As Integer
  126. Sub CancelButton_Click ()
  127.   gfDBOpenFlag = False
  128.   gstDBName = NULL_STR
  129.   gstDataBase = NULL_STR
  130.   gstUserName = NULL_STR
  131.   gstPassword = NULL_STR
  132.   Unload Me
  133. End Sub
  134. Sub cDBName_Click ()
  135.   On Error Resume Next
  136.   Dim tmp As String
  137.   Dim x As Integer
  138.   cDatabase = NULL_STR
  139.   cUserName = NULL_STR
  140.   cPassword = NULL_STR
  141.   'get the database name if there is one
  142.   tmp = String$(255, 32)
  143.   x = OSGetPrivateProfileString(cDBName, "database", NULL_STR, tmp, Len(tmp), "ODBC.INI")
  144.   cDatabase = Mid$(tmp, 1, x)
  145.   'get the last user name is there is one
  146.   tmp = String$(255, 32)
  147.   x = OSGetPrivateProfileString(cDBName, "lastuser", NULL_STR, tmp, Len(tmp), "ODBC.INI")
  148.   cUserName = Mid$(tmp, 1, x)
  149.   cPassword = NULL_STR
  150.   If Len(cUserName) > 0 Then
  151.     cPassword.SetFocus
  152.   Else
  153.     cDatabase.SetFocus
  154.   End If
  155. End Sub
  156. Sub Form_Load ()
  157.   Left = (Screen.Width - Width) / 2
  158.   Top = (Screen.Height - Height) / 2
  159.   GetDataSources cDBName
  160.   cDBName = gstDBName
  161.   cDatabase = gstDataBase
  162.   cUserName = gstUserName
  163.   cPassword = gstPassword
  164.   MsgBar "Enter DataBase Parameters", False
  165.   BeenLoaded = True
  166. End Sub
  167. Sub Form_Paint ()
  168.   Outlines Me
  169. End Sub
  170. Sub Form_Unload (Cancel As Integer)
  171.   MsgBar NULL_STR, False
  172. End Sub
  173. 'this routine fills a list box with all available
  174. 'ODBC data sources found in ODBC.INI
  175. Sub GetDataSources (listctrl As Control)
  176.   Dim DataSource As String, Description As String
  177.   Dim DataSourceLen As Integer, DescriptionLen As Integer
  178.   Dim retcode As Integer
  179.   Dim henv As Long
  180.   If SQLAllocEnv(henv) <> -1 Then
  181.     DataSource = String$(32, 32)
  182.     Description = String$(255, 32)
  183.     'get the first one
  184.     retcode = SQLDataSources(henv, 2, DataSource, Len(DataSource), DataSourceLen, Description, Len(Description), DescriptionLen)
  185.     While retcode = 0 Or retcode = 1
  186.       listctrl.AddItem Mid(DataSource, 1, DataSourceLen)
  187.       DataSource = String$(32, 32)
  188.       Description = String$(255, 32)
  189.       'get all the others
  190.       retcode = SQLDataSources(henv, 1, DataSource, Len(DataSource), DataSourceLen, Description, Len(Description), DescriptionLen)
  191.     Wend
  192.   End If
  193. End Sub
  194. Sub Label1_DblClick ()
  195.   If Len(Label1) = 0 Then
  196.     Label1 = "E"
  197.   Else
  198.     Label1 = NULL_STR
  199.   End If
  200. End Sub
  201. Sub OkayButton_Click ()
  202.    Dim Connect As String, DataSource As String
  203.    Dim x As Integer
  204.    Dim st As String
  205.    Dim i As Integer
  206.    Dim s As String, t As String
  207.    Dim dbq As String
  208.    On Error GoTo OpenError
  209.    MsgBar "Opening DataBase", True
  210.    If VDMDI.PrefOpenOnStartup.Checked = True Then
  211.      Me.Refresh
  212.    End If
  213.    SetHourglass Me
  214.    'check for blank server name and clear other parms
  215.    If Len(cDBName) = 0 Then
  216.      cDatabase = NULL_STR
  217.      cUserName = NULL_STR
  218.      cPassword = NULL_STR
  219.    End If
  220.    'build connect string
  221.    Connect = "ODBC;"
  222.    If Len(cUserName) > 0 Then
  223.      Connect = Connect & "UID=" & cUserName & ";PWD=" & cPassword
  224.    End If
  225.    If Len(cDatabase) > 0 Then
  226.      Connect = Connect & ";DATABASE=" & cDatabase
  227.    End If
  228.    'add login timeout
  229.    Connect = Connect & ";LoginTimeout=" & glLoginTimeout
  230.    If Label1 = "E" Then Connect = Connect & ";APP=Einstein"
  231.    DataSource = cDBName
  232.    'save the values
  233.    gstDBName = cDBName
  234.    gstDataBase = cDatabase
  235.    gstUserName = cUserName
  236.    gstPassword = cPassword
  237.    gstDataType = SQLDB
  238.    Me.Hide
  239.    Set gCurrentDB = OpenDatabase(DataSource, False, False, Connect)
  240.    If gfDBOpenFlag = True Then
  241.      CloseAllDynasets
  242.    End If
  243.    gfTransPending = False
  244.    VDMDI.ToolBar.Visible = True
  245.    VDMDI.QueryBuilder.Visible = True
  246.    VDMDI.TblAttach.Visible = False
  247.    fSQL.CreateQueryDefbtn.Visible = False
  248.    'process the connect string just in case the
  249.    'values came from the ODBC dialogs
  250.    t = gCurrentDB.Connect
  251.    If InStr(t, "=") Then
  252.      i = 1
  253.      While i <= Len(t) + 1
  254.        If Mid(t, i, 1) = ";" Or i = Len(t) + 1 Then
  255.          If Len(s) > 0 And InStr(s, "=") > 0 Then
  256.            Select Case Mid(s, 1, InStr(1, s, "=") - 1)
  257.              Case "DSN"
  258.                gstDBName = Mid(s, InStr(1, s, "=") + 1, Len(s))
  259.              Case "DATABASE"
  260.                gstDataBase = Mid(s, InStr(1, s, "=") + 1, Len(s))
  261.              Case "DBQ"
  262.                gstDataBase = Mid(s, InStr(1, s, "=") + 1, Len(s))
  263.              Case "UID"
  264.                gstUserName = Mid(s, InStr(1, s, "=") + 1, Len(s))
  265.              Case "PWD"
  266.                gstPassword = Mid(s, InStr(1, s, "=") + 1, Len(s))
  267.               Case Else
  268.                'nothing
  269.            End Select
  270.          End If
  271.          s = NULL_STR
  272.        Else
  273.          s = s + Mid(t, i, 1)
  274.        End If
  275.        i = i + 1
  276.      Wend
  277.    End If
  278.    cDBName = gstDBName
  279.    cDatabase = gstDataBase
  280.    cUserName = gstUserName
  281.    cPassword = gstPassword
  282.    x = OSWritePrivateProfileString(gstDBName, "Database", gstDataBase, "ODBC.INI")
  283.    x = OSWritePrivateProfileString(gstDBName, "LastUser", gstUserName, "ODBC.INI")
  284.    fTables.Caption = gstDBName & "." & gstDataBase
  285.    gCurrentDB.QueryTimeout = glQueryTimeout
  286.    'success
  287.    gfDBOpenFlag = True
  288.    ResetMouse Me
  289.    Unload Me
  290.    GoTo OkayEnd
  291. OpenError:
  292.    ResetMouse Me
  293.    gfDBOpenFlag = False
  294.    If Len(cDBName) > 0 Then
  295.      If InStr(1, Error$, "Data source not found") > 0 Then
  296.        Beep
  297.        MsgBox "This DataBase has not been Registered, this will now be attempted for you!", 48
  298.        cDatabase = NULL_STR
  299.        cUserName = NULL_STR
  300.        cPassword = NULL_STR
  301.        If RegisterDB((cDBName)) = True Then
  302.          MsgBox "'" & cDBName & "' has been Registered, proceed with Open.", 48
  303.        End If
  304.      ElseIf InStr(1, Error$, "Login failed") > 0 Then
  305.        Beep
  306.        MsgBox "Invalid Parameter(s), Please try again!", 48
  307.      ElseIf InStr(1, Error$, "QueryTimeout property") > 0 Then
  308.        If glQueryTimeout <> 5 Then
  309.          Beep
  310.          MsgBox "Query Timeout Could not be set, default will be used!", 48
  311.        End If
  312.        Resume Next
  313.      Else
  314.        ShowError
  315.      End If
  316.    End If
  317.    MsgBar "Enter DataBase Parameters", False
  318.    Me.Show MODAL
  319.    Resume OkayEnd
  320. OkayEnd:
  321. End Sub
  322. Function RegisterDB (dbname As String) As Integer
  323.    On Error GoTo RDBErr
  324.    Dim driver As String
  325.    driver = InputBox("Enter Driver Name from ODBCINST.INI File:", "Driver Name", DEFAULTDRIVER)
  326.    If driver <> DEFAULTDRIVER Then
  327.      RegisterDatabase cDBName, driver, False, NULL_STR
  328.    Else
  329.      RegisterDatabase cDBName, driver, True, NULL_STR
  330.    End If
  331.    RegisterDB = True
  332.    GoTo RDBEnd
  333. RDBErr:
  334.    RegisterDB = False
  335.    Resume RDBEnd
  336. RDBEnd:
  337. End Function
  338.